package com.zhuiyun.project.api.sysarticle.mapper;

import com.zhuiyun.project.api.friend.entity.Friend;
import com.zhuiyun.project.api.friend.model.FriendModel;
import com.zhuiyun.project.api.joingroup.model.JoinGroupModel;
import com.zhuiyun.project.api.sysarticle.model.ArticleModel;
import com.zhuiyun.project.api.sysarticle.model.CommentModel;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @ClassName ArticleMapper
 * @Description TODO
 * @Author 韩佳轩
 * @Date 2023/6/15 10:35
 **/
@Mapper
public interface ArticleMapper {
    /**
     * @param articleModel
     * @Author 韩佳轩
     * @Description app首页文章查询
     * @Date 2023/6/15 11:08
     * @Return
     */
        @Select("<script>select au.user_name as userName,au.image_id as imageId,a.*,s.dict_label as dictLabel " +
            "from app_physician ap,article a,sys_dict_data s,app_user au " +
            "where ap.id=a.user_id and a.label_id=s.data_id and au.id=ap.user_id" +
            "<when test='articleTitle!=&quot;&quot; and articleTitle!=null'>" +
            "and a.article_title like concat('%',#{articleTitle},'%') </when> and a.is_hidden=2 and a.del_flag=0 and a.approval_status=1 and is_published=1</script>")
    List<ArticleModel> getArticleAll(ArticleModel articleModel);

    /**
     * @Author 小江
     * @Description //TODO 根据病种查询
     * @Date 21:18 2023/7/11
     **/

//    @Select("<script>select ap.doctor_name as doctorName,au.image_id as imageId,a.*,s.dict_label as dictLabel " +
//            "from app_physician ap,article a,sys_dict_data s,app_user au " +
//            "where ap.id=a.user_id and a.label_id=s.data_id and au.id=ap.user_id" +
//            "<when test='articleTitle!=&quot;&quot; and articleTitle!=null'>" +
//            "and a.article_title like concat('%',#{articleTitle},'%') </when> and a.is_hidden=2 and a.del_flag=0 and a.approval_status=1</script>")
    @Select("<script>SELECT " +
            "a.*, " +
            "d.user_name AS userName, " +
            "c.data_id ," +
            "d.image_id As imageId " +
            "FROM " +
            "article a, " +
            "app_physician b, " +
            "sys_dict_data c ," +
            "app_user d" +
            "<where><when test='articleTitle!=&quot;&quot; and articleTitle!=null'> " +
            "a.article_title like concat('%',#{articleTitle},'%')</when> <when test='labelId!=&quot;&quot; and labelId!=null'> and a.label_id = #{labelId} </when> and a.is_hidden = 2 and a.del_flag=0 and a.approval_status = 1 and  a.is_published = 1 and " +
            " a.user_id = b.id and a.label_id = c.data_id  and b.user_id = d.id</where></script>")
    List<ArticleModel> ArticleLater(ArticleModel articleModel);

    /**
     * @param articleModel
     * @Author 韩佳轩
     * @Description 后台管理文章查询
     * @Date 2023/6/15 11:08
     * @Return
     */
    @Select("<script>select au.user_name as userName,au.image_id as imageId,a.*,s.dict_label as dictLabel " +
            "from app_physician ap,article a,sys_dict_data s,app_user au " +
            "where ap.id=a.user_id and a.label_id=s.data_id and au.id=ap.user_id" +
            "<when test='articleTitle!=&quot;&quot; and articleTitle!=null'>" +
            "and a.article_title like concat('%',#{articleTitle},'%') </when> and a.del_flag=0 ORDER BY a.create_time desc </script>")
    List<ArticleModel> getSysArticleAll(ArticleModel articleModel);

    /**
     * @param id
     * @Author 韩佳轩
     * @Description 下架文章
     * @Date 2023/6/15 16:45
     * @Return int
     */
    @Update("update article set del_flag=1,update_time=now() where id=#{id}")
    int deleteArticle(int id);

    /**
     * @param
     * @Author 韩佳轩
     * @Description app小程序首页跳转文章详情&医生的文章列表跳转详情 医师表与文章表(主表)
     * @Date 2023/6/19 14:24
     * @Return
     */

    @Select("<script>SELECT a.*,au.user_name as userName,s.dict_label as dictLabel,au.image_id as imageId," +
            "ap.hospital,ap.department,ap.title "   +
            "FROM article a,app_physician ap,sys_dict_data s,app_user au " +
            "WHERE a.id=#{id} and ap.id = #{userId} and a.label_id=s.data_id and au.id=ap.user_id</script>")
    List<ArticleModel> ArticleMain(ArticleModel articleModel);


    /**
     * @param
     * @Author 韩佳轩
     * @Description app小程序医师的文章列表
     * @Date 2023/6/20 9:50
     * @Return
     */
    @Select("<script>SELECT a.*,au.user_name,au.image_id " +
            "FROM article a,app_physician ap,app_user au " +
            "WHERE a.user_id=#{userId} and ap.id=#{userId} and au.id=ap.user_id and a.del_flag=0 </script>")
    List<ArticleModel> ArticleDoctorList(ArticleModel articleModel);


    /**
     * @param commentModel
     * @Author 韩佳轩
     * @Description app评论全查
     * @Date 2023/6/21 14:49
     * @Return
     */
    @Select("SELECT ac.*,au.user_name as userName,au.image_id as imageId,au.user_phone as userPhone " +
            "from app_user au,app_comment ac,article a " +
            "WHERE ac.article_id=a.id and au.id=ac.user_id and ac.article_id=#{articleId} ")
    List<CommentModel> CommentAll(CommentModel commentModel);

    /**
     * @param commentModel
     * @Author 韩佳轩
     * @Description app添加评论
     * @Date 2023/6/21 20:12
     * @Return
     */
    @Insert("insert into app_comment(user_id,article_id,create_time,comment_content)" +
            "values(#{userId},#{articleId},#{createTime},#{commentContent}) ")
    int insertComment(CommentModel commentModel);

    /**
     * @param articleModel
     * @Author 韩佳轩
     * @Description app文章权限私密 （公开默认全查）
     * @Date 2023/6/30 9:03
     * @Return
     */
    @Update("update article set is_hidden=#{isHidden},update_time=now() where id=#{id}")
    int Privacy(ArticleModel articleModel);

    /**
     * @param friendmodel
     * @Author 韩佳轩
     * @Description 转发文章的好友列表
     * @Date 2023/7/14 11:19
     * @Return
     */
    @Select("<script>SELECT a.image_id as imageId,ar.images as images,ar.video as video,f.*,a.user_name as userName " +
            "FROM friend f,app_user a,article ar where f.friend_id=a.id and f.user_id=#{userId} and ar.id=#{id}" +
            "<when test='remarkName!=&quot;&quot; and remarkName!=null'>" +
            "and f.remark_name like concat('%',#{remarkName},'%') </when> " +
            "<when test='remarkName!=&quot;&quot; and remarkName!=null'>" +
            "or a.user_name like concat('%',#{remarkName},'%') </when> " +
            "and f.del_flag=0 </script>")
    List<FriendModel> FriendList(FriendModel friendmodel);

   /**
   * @Author 韩佳轩
   * @Description  转发文章的群聊列表
   * @Date 2023/7/31 16:54
   * @Return java.util.List<com.zhuiyun.project.api.friend.model.FriendModel>
   * @param  joinGroupModel
   */
    @Select("<script>SELECT ar.images AS images,ar.video AS video,g.group_name AS groupName,g.group_img_id AS groupImgId,j.*" +
            "FROM article ar,join_group j,group_chat g " +
            "WHERE ar.id=#{id} and j.group_id = g.id AND j.user_id = #{userId} AND j.user_type = 1 AND g.del_flag = 0" +
            "<when test='groupName!=&quot;&quot; and groupName!=null'>" +
            "and g.group_name like concat('%',#{groupName},'%') </when> </script>")
    List<JoinGroupModel> GroupList(JoinGroupModel joinGroupModel);
}
